package com.project.controller.userController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.ShopBean;
import com.project.bean.UserBean;
import com.project.service.userService.IUserService;
import com.project.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService service;
    @RequestMapping("/login")
    public UserBean login(String userName, String pwd, HttpServletRequest request){
        UserBean user = service.login(userName, pwd);
        if(user == null){
            return null;
        }
        request.getSession().setAttribute("loginUser", user);
        Map<Integer, ShopBean> shopMap = new HashMap<>();
        request.getSession().setAttribute("ShopMap",shopMap);
        return user;
    }
    @RequestMapping("/updateP")
    public String updateP(int userId,String newPwd){
        service.updateP(userId, newPwd);
        return "ok";
    }

    @RequestMapping("getUser")
    public UserBean login( HttpServletRequest request) throws Exception {
        UserBean user=(UserBean)request.getSession().getAttribute("loginUser");
        return user;
    }

    @RequestMapping("/updateInfo")
    public String updateP(int id,String name, String tel){
        service.updateInfo(id, name, tel);
        return "ok";
    }

    @RequestMapping("/add")
    public String add(UserBean user,@RequestParam("face") MultipartFile mf) throws Exception {
        String fileName = UploadUtil.upload(mf,"static/html/img");
        user.setFaceImg(fileName);
        service.add(user);
        return "ok";
    }
}
